package com.sdhs.analyze.service.monitor;

import com.alibaba.fastjson.JSONObject;
import com.sdhs.analyze.bean.DicNodeType;
import com.sdhs.analyze.bean.DicSpecialType;
import com.sdhs.analyze.dao.dic.DicNewMapper;
import com.sdhs.analyze.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DicServiceNew {

    @Autowired
    private DicNewMapper dicMapper;

    @Autowired
    private RedisUtil redisUtil;

    private static Date dicNodeTypeLastRefreshTime = null;

    private static Map<String, DicNodeType> nodeTypeMap = null;

    public Map<String, DicNodeType> getNodeType() {

        List<DicNodeType> list = null;

        /* 计算五分钟前的时间 */
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());

        calendar.add(Calendar.MINUTE, -5);

        if (nodeTypeMap != null && dicNodeTypeLastRefreshTime.after(calendar.getTime())) {

            return nodeTypeMap;
        } else {

            nodeTypeMap = new HashMap<>();

            dicNodeTypeLastRefreshTime = new Date();

            list = dicMapper.getDicNodeTypeList();

            if (list != null) {
                for (DicNodeType dic : list) {

                    nodeTypeMap.put(dic.getNodeType(), dic);
                }
            }
            return nodeTypeMap;
        }
    }

    private static Date dicSpecialTypeRefreshTime = null;

    private static Map<String, DicSpecialType> statedDicSpecialTypeMap = null;

    public Map<String, DicSpecialType> getStatedDicSpecialType() {

        /* 计算五分钟前的时间 */
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());

        calendar.add(Calendar.MINUTE, -5);

        if (statedDicSpecialTypeMap == null || statedDicSpecialTypeMap.size() == 0 || dicSpecialTypeRefreshTime.after(calendar.getTime())) {

            Map<String, DicSpecialType> tmpMap = null;

            String key = "stated_special_type";

            String jsonString = redisUtil.get(key);

            if (jsonString == null) {

                tmpMap = new HashMap<>();

                /* 从数据库取 */
                List<DicSpecialType> dicSpecialTypeList = dicMapper.selectStatedDicSpecialType();

                if (dicSpecialTypeList != null) {

                    for (DicSpecialType dicSpecialType : dicSpecialTypeList) {

                        tmpMap.put(dicSpecialType.getSpecialType(), dicSpecialType);
                    }
                    /* 缓存 */
                    redisUtil.set(key, JSONObject.toJSONString(tmpMap));

                    statedDicSpecialTypeMap = tmpMap;

                    dicSpecialTypeRefreshTime = new Date();
                }
            } else {

                tmpMap = (Map<String, DicSpecialType>) JSONObject.parseObject(jsonString, HashMap.class);

                for (Map.Entry<String, DicSpecialType> element : tmpMap.entrySet()) {

                    Object tmp = element.getValue();

                    tmp = JSONObject.parseObject(JSONObject.toJSONString(tmp), DicSpecialType.class);

                    tmpMap.put(element.getKey(), (DicSpecialType) tmp);
                }

                if (statedDicSpecialTypeMap == null) {

                    dicSpecialTypeRefreshTime = new Date();
                }

                statedDicSpecialTypeMap = tmpMap;
            }
        }
        return statedDicSpecialTypeMap;
    }
}
